import { h, render } from 'vue'
import SearchBar from '@/views/SearchBar.vue'
class SearchBarCreactor {
    isShow: boolean
    container: HTMLDivElement
    static instance: SearchBarCreactor
    static getInstance() {
        if (!SearchBarCreactor.instance) {
            SearchBarCreactor.instance = new SearchBarCreactor()
        }
        return SearchBarCreactor.instance
    }
    constructor() {
        this.isShow = false
        this.container = document.createElement('div')
    }
    present() {
        if (this.isShow) {
            this.dismiss()
        } else {
            const searchBarVNode = h(SearchBar)
            render(searchBarVNode, this.container)
            document.body.insertBefore(this.container, document.body.firstChild)
            this.isShow = true
        }
    }
    dismiss() {
        if (this.container) {
            render(null, this.container)
            document.body.removeChild(this.container)
            this.isShow = false
        }
    }
}
export const useSearchBar = () => {
    const searchBar = SearchBarCreactor.getInstance()
    /**
     * 打开搜索框
     * @returns
     */
    const openSearchBar = () => {
        searchBar.present()
    }
    /**
     * 关闭搜索框
     */
    const closeSearchBar = () => {
        searchBar.dismiss()
    }
    
    return {
        openSearchBar,
        closeSearchBar,
    }
}
